package chapter3.eg3;

import chapter3.eg2.Customer;
import java.net.MalformedURLException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

public class AvroSerializerApp {

  public static void main(String[] args) throws MalformedURLException, InterruptedException {

    Properties properties = new Properties();
    properties.put("bootstrap.servers", "localhost:9092");
    properties.put("key.serializer", "io.confluent.kafka.serializers.KafkaAvroSerializer");
    properties.put("value.serializer", "io.confluent.kafka.serializers.KafkaAvroSerializer");
    properties.put("schema.registry.url", "http://localhost:8081");

    Producer<String, Customer> producer = new KafkaProducer<>(properties);
    while (true) {
      Customer customer = new Customer(1, "zhangsan");
      System.out.println("Generated customer " + customer);
      ProducerRecord<String, Customer> record = new ProducerRecord<>("customerContacts",
          String.valueOf(customer.getId()), customer);
      producer.send(record);
      TimeUnit.SECONDS.sleep(1);
    }
  }

}
